package com.indoors.sample;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.indoors.chart.RelationChartLayout;

import java.util.Random;

public class SampleAdapterActivity extends AppCompatActivity {

    private Random mRandom;
    private SparseArray<Float> mWeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample_adapter);
        findViewById(R.id.chart_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(SampleAdapterActivity.this, SampleLayoutActivity.class);
                startActivity(i);
            }
        });
        RelationChartLayout rcl = findViewById(R.id.rcl);
        mRandom = new Random(System.currentTimeMillis());
        mWeight = new SparseArray<>();
        rcl.setAdapter(new RelationChartLayout.ChartAdapter() {

            @Override
            protected float getItemDistance(int position) {
                return mRandom.nextFloat();
            }

            @Override
            protected float getItemWeight(int position) {
                if (mWeight.get(position) == null) {
                    mWeight.put(position, mRandom.nextFloat());
                }
                return mWeight.get(position);
            }

            @Override
            public int getCount() {
                return 10;
            }

            @Override
            protected View getView(int position) {
                TextView v = new TextView(SampleAdapterActivity.this);
                v.setText("结点 ：" + position);
                v.setTextColor(Color.parseColor("#DDDDDD"));
                v.setGravity(Gravity.CENTER);
                v.setPadding(4,4,4,4);
                v.setTextSize(8 + 5 * getItemWeight(position));
                GradientDrawable gd = new GradientDrawable();
                gd.setShape(GradientDrawable.OVAL);
                gd.setColor((int)(Math.abs(mRandom.nextLong()) % 0xFFFFFF | 0xFF000000));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    v.setBackground(gd);
                }
                return v;
            }
        });
    }
}
